package com.sihan.framework.etm.common.util;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import lombok.extern.slf4j.Slf4j;


/**
 * 字符串处理类
 * 
 * @author gsl
 *
 */
@Slf4j
public class StringUtils extends org.apache.commons.lang3.StringUtils {
	/**
	 * 首字母小写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstToLower(String str) {
		StringBuilder sb = new StringBuilder(str);
		sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * 首字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstToUpper(String str) {
		StringBuilder sb = new StringBuilder(str);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * 日期转化字符串
	 * 
	 * @param date
	 * @param string
	 * @return
	 */
	public static String dateToString(Date date, String s) {
		String sdate = "";
		Locale locale = Locale.CHINA;
		DateFormat format = null;
		if (Pattern.matches("\\w{4}\\w{1,2}\\w{1,2}", s)) {
			format = new SimpleDateFormat("yyyyMMdd");
		} else if (Pattern.matches("\\w{4}.\\w{1,2}.\\w{1,2}", s)) {
			format = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
		} else if (Pattern.matches("\\w{2}.\\w{2}.\\w{2}", s)) {
			format = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
		} else if (Pattern.matches("\\w{4}.\\w{1,2}.\\w{1,2}[ ]\\w{1,4}.\\w{1,2}.\\w{1,2}", s)) {
			format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
		} else {
			format = new SimpleDateFormat("yyyyMMddHHmmss");
		}
		sdate = format.format(date);
		return sdate;
	}

	/**
	 * 返回按年月日生成的文件路径<br/>
	 * yyyy/mm/dd
	 * 
	 * @return
	 */
	public static String getDateFilePath() {
		Calendar calendar = Calendar.getInstance();
		String filePath = calendar.get(Calendar.YEAR) + File.separator + (calendar.get(Calendar.MONTH) + 1)
				+ File.separator + calendar.get(Calendar.DAY_OF_MONTH);
		return filePath;
	}

	/**
	 * 字符串转化日期
	 * 
	 * @param s
	 * @return
	 */
	public static Date String2Date(String s) {
		Date date = null;
		Locale locale = Locale.CHINA;
		DateFormat format = null;
		if (Pattern.matches("\\d{4}.\\d{1,2}", s)) {
			format = new SimpleDateFormat("yyyy-MM");// 前台只传入了年-月，进行时间补全
		} else if (Pattern.matches("\\d{4}.\\d{1,2}.\\d{1,2}", s)) {
			format = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
		} else if (Pattern.matches("\\d{2}.\\d{2}.\\d{2}", s)) {
			format = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
		} else if (Pattern.matches("\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}.\\d{1,2}.\\d{1,2}", s)) {
			format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
		} else {
			format = new SimpleDateFormat("yyyy-MM-dd");
		}
		try {
			date = format.parse(s);
		} catch (ParseException e) {
			log.error("异常",e);
		}
		return date;
	}

	/**
	 * @msg 获取随即数 请先清除 StringUtils.map
	 * @author dejun
	 * @return
	 */
	public static String getNo() {
		return getDayNo() + getLenthStr(3) + getLenthStr(1);
	}

	private static String getDayNo() {
		Date today = new Date();
		SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
		return f.format(today) + getLenthStr(3) + getLenthStr(3);
	}

//	private static String randomNumStr() {
	// return getLenthStr(4);
//		Integer i = (int) (Math.random() * 10000);
//		if (i > 1000)
//			return i.toString();
//		else if (i < 1000 && i >= 100) {
//			return getLenthStr(1) + i.toString();
//		} else if (i < 100 && i >= 10) {
//			return getLenthStr(2) + i.toString();
//		} else
//			return getLenthStr(3) + i.toString();
//		String ts = "";
//		int n = 10;
//		Random rand = new Random();
//		boolean[] bool = new boolean[n];
//		int randInt = 0;
//		for (int i = 0; i < 6; i++) {
//			do {
//				randInt = rand.nextInt(n);
//			} while (bool[randInt]);
//			bool[randInt] = true;
//		//	System.out.println(randInt);
//			ts += randInt;
//		}
//		return ts;
//	}

	private static String getLenthStr(int i) {
		if (i == 1) {
			return new Random().nextInt(10) + "";
		} else {
			return new Random().nextInt(10) + "" + getLenthStr(i - 1);
		}
	}

	/**
	 * 新数组转为字符串
	 * 
	 * @param objs 基本类型+String的数组
	 * @return
	 */
	public static String arrayToString(Object[] objs) {
		String str = Arrays.toString(objs);
		return str.replaceAll("\\[", "").replaceAll("\\]", "");
	}

	/**
	 * 获取工单编号
	 * 
	 * @return
	 */
	public static String getOrderNo() {
		Date today = new Date();
		SimpleDateFormat f = new SimpleDateFormat("yyyy");
		return "SGCM" + f.format(today) + getLenthStr(8);
	}

	// 过滤特殊字符
	public static String StringFilter(String str) throws PatternSyntaxException {
		if(org.apache.commons.lang3.StringUtils.isBlank(str)) {
			return str;
		}
		// 只允许字母和数字
		// String regEx ="[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？′]";
		try {
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(str);
			return m.replaceAll("").trim();
		}catch(Exception e) {
			return str;
		}
	}
	
	public static void main(String[] args) throws IOException {
//		String encodeStr = "XG1JHvFFx3cpxjcUDDVePQ==";
//		Decoder decoder = Base64.getDecoder();
//		byte[] bytes = decoder.decode(encodeStr);
//	    System.out.println("BASE64解密：" + new String(bytes));
//	    System.out.println(SM4.decode(encodeStr, SM4.getKey()));
//		String needFilter = "220ky东平一线261开关由热备用转检修&#39;&prime;]|";
//		needFilter = StringEscapeUtils.unescapeJava(needFilter);
//		needFilter = StringEscapeUtils.unescapeHtml(needFilter);
//		System.out.println(needFilter);
//		String aaa = com.sihan.framework.etm.common.util.StringUtils.StringFilter(needFilter);
//		System.out.println(aaa);
//		aaa.replaceAll("[^a-zA-Z0-9\\u4E00-\\u9FA5]", "");
//		System.out.println(aaa);
//		aaa.replaceAll("ky", "KV");
//		System.out.println(aaa);
		
		String content = "220ky东平一线261开关由热备";
		String pattern = "kv|ky";
		Pattern p = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		System.out.println(m.replaceAll("kV").trim());
	}
}
